package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class UpdatepwdController {
    @Autowired
    private IEmployeeService employeeService;
    @RequestMapping("/updatepwd")
    @ResponseBody
    public JsonResult updatepwd(String oldPassword, String newPassword, HttpSession session){
        //拿到当前用户 可以用提取的工具类
        Employee emp = UserContext.getCurrentUser();
        final Long id = emp.getId();
        Employee employee = null;
        try {
            employee = employeeService.selectPassword(id,oldPassword);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }
        if (employee!=null){
            employeeService.updatePassword(id,newPassword);
        }
        return  new JsonResult();
    }
}
